package com.haust.leyou.service;

import com.haust.leyou.mapper.SpecGroupMapper;
import com.haust.leyou.mapper.SpecParamMapper;
import com.haust.leyou.pojo.SpecGroup;
import com.haust.leyou.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: csp1999
 * @Date: 2020/12/10/20:30
 * @Description:
 */
@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 根据分类id查询规格组
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return specGroupMapper.select(specGroup);
    }

    /**
     * 根据gid,cid,generic,searching 查询规格参数
     *
     * @param gid       商品规格组gid
     * @param cid       商品分类cid
     * @param generic   是否是sku通用属性，true或false
     * @param searching 是否用于搜索过滤，true或false
     * @return
     */
    public List<SpecParam> queryParamsByGid(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setGeneric(generic);
        specParam.setSearching(searching);
        return specParamMapper.select(specParam);
    }

    /**
     * 根据cid 查询SpecGroup集合
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsWithParam(Long cid) {
        List<SpecGroup> groups = queryGroupsByCid(cid);

        groups.forEach(group -> {
            List<SpecParam> params = queryParamsByGid(group.getId(), null, null, null);
            group.setParams(params);
        });

        return groups;
    }
}
